package cn.edu.ldu.views.operator;

import cn.edu.ldu.controller.UserController;
import cn.edu.ldu.entity.ChatHistoryEntity;
import cn.edu.ldu.util.Consts;
import cn.edu.ldu.util.Translate;
import cn.edu.ldu.views.controller.MainUIController;
import cn.edu.ldu.views.main.MainUI;
import com.alibaba.fastjson.JSONObject;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author DYZ
 */
public class History extends javax.swing.JFrame {

    //获得历史记录
    HashMap<String,Object> content;
    /**
     * Creates new form History
     */
    public History(List<ChatHistoryEntity> chatHistory) {
        this.content = content;
        initComponents(chatHistory);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents(List<ChatHistoryEntity> chatHistory) {

        jPanel1 = new javax.swing.JPanel();
        txtSearch = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listHistory = new javax.swing.JList<>();
        history = new DefaultListModel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("搜索关键词"));

        txtSearch.setText(" ");

        btnSearch.setText("搜索");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(67, 67, 67)
                                .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(btnSearch)
                                .addContainerGap(80, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnSearch))
                                .addContainerGap(24, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        //获得历史记录
        for(int i = 0;i<chatHistory.size();i++){
            if(chatHistory.get(i).getSenderId().equals(UserController.getUserEntity().getId())){
                history.addElement(Translate.stampToDate(chatHistory.get(i).getSendTime())+"  " + UserController.getUserEntity().getUsername() +":");
            }else {
                history.addElement(Translate.stampToDate(chatHistory.get(i).getSendTime())+"  " + MainUIController.getMainUI().getTalkerName() +":");
            }
            history.addElement(chatHistory.get(i).getContent());
        }
        listHistory.setModel(history);
        jScrollPane1.setViewportView(listHistory);
//        listHistory.setModel(new javax.swing.AbstractListModel<String>() {
//            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
//            public int getSize() { return strings.length; }
//            public String getElementAt(int i) { return strings[i]; }
//        });
//        jScrollPane1.setViewportView(listHistory);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>


    // Variables declaration - do not modify
    private javax.swing.JButton btnSearch;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList<String> listHistory;
    private javax.swing.JTextField txtSearch;
    DefaultListModel history;
    // End of variables declaration
}
